[dolphinflow86] WEEK 07 Solutions - #2799
Conversation
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
longest-substring-without-repeating-characters/dolphinflow86.py
# N is the length of s.
# TC: O(N) - each character is added and removed at most once
# SC: O(N) - stores the characters in the current window
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
chars = set()
left = 0
longest = 0
for right, char in enumerate(s):
while char in chars:
chars.remove(s[left])
left += 1
chars.add(char)
longest = max(longest, right - left + 1)
return longest
- ํจํด: Sliding Window, Hash Map / Hash Set
- ์ค๋ช : ๋ฌธ์์ด์์ ์ค๋ณต ์ ๊ฑฐ๋ฅผ ์ํด ์ฐฝ์ ์์ง์ด๋ฉฐ(left, right) ํ์ฌ ์๋์ฐ์ ๋ฌธ์๋ค์ ์งํฉ์ ์ ์ฅํ๊ณ , ์ค๋ณต ์ ์ผ์ชฝ ํฌ์ธํฐ๋ฅผ ์ด๋์ํค๋ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ํจํด์ ์ฌ์ฉํฉ๋๋ค. ํด์ ์ธํธ๋ฅผ ์ด์ฉํด ๋ฌธ์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(min(n, m)) |
ํผ๋๋ฐฑ: ํด๋น ๊ตฌํ์ ๋ชจ๋ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ ์ถ๊ฐ/์ ๊ฑฐํ๋ฉฐ ์๋์ฐ๋ฅผ ์ด๋์ํค๋ฏ๋ก ์ ํ ์๊ฐ๊ณผ ์ฐฝ ํฌ๊ธฐ์ ๋น๋กํ ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๐ dolphinflow86 ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
number-of-islands/dolphinflow86.py
# R is the number of rows, and C is the number of columns.
# TC: O(R * C) - visits each cell at most once
# SC: O(R * C) - uses the recursion stack in the worst case
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
rows = len(grid)
cols = len(grid[0])
def dfs(row, col):
if (
row < 0
or row >= rows
or col < 0
or col >= cols
or grid[row][col] != "1"
):
return
grid[row][col] = "0"
dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
islands = 0
for row in range(rows):
for col in range(cols):
if grid[row][col] == "1":
islands += 1
dfs(row, col)
return islands
- ํจํด: Depth-First Search, Backtracking
- ์ค๋ช : ๊ทธ๋ฆฌ๋์์ 1๋ก ์ฐ๊ฒฐ๋ ์์ญ์ DFS๋ก ํ์ํ๋ฉฐ ๋ฐฉ๋ฌธํ ๋ ธ๋๋ฅผ 0์ผ๋ก ๋ฐ๊ฟ ์ฐ๊ฒฐ ์์(์ฌ)์ ๊ฐ์๋ฅผ ์ . ์ฌ๊ท๋ฅผ ์ด์ฉํ ๊น์ ํ์์ด ํต์ฌ ํจํด์ ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(R * C) | O(rows * cols) | โ |
| Space | O(R * C) | O(rows * cols) | โ |
ํผ๋๋ฐฑ: ๊ทธ๋ฆฌ๋์ ๊ฐ ์ ์ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๊ณ ์ธ์ ํ '1'๋ค์ ์ฌ๊ท์ ์ผ๋ก ์ฒ๋ฆฌํ๋ค.
๊ฐ์ ์ ์: ์ฌ๊ท ๊น์ด๊ฐ ์ปค์ง ์ ์๋ ํ๊ฒฝ์์๋ ์คํ ๊ธฐ๋ฐ DFS๋ BFS๋ก ์คํ ์ฌ์ฉ์ ๋ช ์ํ๋ ๊ฒ์ด ์์ ํ๋ค.
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
reverse-linked-list/dolphinflow86.py
# TC: O(N) - visits each node exactly once
# SC: O(1) - reverses the links in place
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev
- ํจํด: Two Pointers, Linked List
- ์ค๋ช : ์ฃผ์ด์ง ์ฝ๋๋ ๋จ์ํ ํฌ์ธํฐ ๋ ๊ฐ๋ฅผ ์ฌ์ฉํด ๋งํฌ๋ ๋ฆฌ์คํธ์ ๋ฐฉํฅ์ ๋ฐ๋๋ก ๋ฐ๊พธ๋ ๊ณผ์ ์ผ๋ก, ๋ ธ๋๋ฅผ ์ํํ๋ฉฐ ๋ ธ๋์ ๋งํฌ๋ฅผ ๋ค์ง๋ ๋ฐ ๋ ํฌ์ธํฐ๋ฅผ ํ์ฉํ๋ ํจํด์ด ํต์ฌ์ ๋๋ค. ์๊ฐ ๋ณต์ก๋ O(N), ๊ณต๊ฐ ๋ณต์ก๋ O(1)๋ก ๊ตฌํ๋ฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ์ฐ๊ฒฐ ๊ตฌ์กฐ๋ฅผ ์ญ์ ์ํค๋ฉด์ ์ํธ ์ฐธ์กฐ๋ฅผ ์ ์งํ๋ ํ์ค ํจํด์ด๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
์ ๊ฐ์ธ์ ์ธ ์๊ฐ์๋์! head๋ฅผ ๋ฆฌํด ํน์ ์ด๋์๋ ์ฐ์ง ์์ผ๋ current๋ฅผ ์ฐ์๋ ๋ถ๋ถ ๊ทธ๋๋ก head๋ฅผ ์ฐ์
๋ ๋์ง ์์๊น?
ํ๋ ์๊ฐ์ด ๋ค์ด์!
๊ทธ๋ฆฌ๊ณ ํ์ด์ฌ์ a, b = b, a์ฒ๋ผ ์ธ์ ์๋ ๋ฌธ๋ฒ์ ํ์ฉํ์๋ฉด while loop ์์ 1์ค๋ก ์ค์ด์ค์๋ ์๋ต๋๋ค!
There was a problem hiding this comment.
์ค ๊ทธ๋ ๋ค์!
๋ฆฌ๋ทฐ ๋ฐ์ผ๋ฉด์ ํ์ด์ฌ ๋ฌธ๋ฒ์ ์กฐ๊ธ์ฉ ์ต์ํด์ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฆฌ๋ทฐ ๊ฐ์ฌํฉ๋๋ค.
parkhojeong
left a comment
There was a problem hiding this comment.
์๊ณ ํ์ จ์ต๋๋ค.
| if ( | ||
| row < 0 | ||
| or row >= rows | ||
| or col < 0 | ||
| or col >= cols | ||
| or grid[row][col] != "1" | ||
| ): |
There was a problem hiding this comment.
python์ a <= x < b ์ฐ๋ฉด ์ด๋ฐ ํํ๋ ๊ฐ๋ฅํ๋ฐ or ์ฌ์ฉํด์ฃผ์ ๊ฒ ๋ ๋ช
ํํ ๊ฑฐ ๊ฐ๊ธฐ๋ ํ๋ค์.
if not (
0 <= row < rows
and 0 <= col < cols
and grid[row][col] == "1"
):
| rows = len(grid) | ||
| cols = len(grid[0]) |
There was a problem hiding this comment.
rows, cols๊ฐ ๊ธธ์ด๋ก ๋ณด์ด์ง ์๊ณ ๋ฐฐ์ด ๊ฐ์ ๋ณ์๊ฐ ๋ด๊ธฐ๋ ๊ฑฐ์ฒ๋ผ ๋ณด์ด๋ ๊ฑฐ ๊ฐ์ต๋๋ค. ๊ธธ์ด๋ฅผ ๋ํ๋ด๋ ๋ค์ด๋ฐ์ ์ฌ์ฉํ์๋ฉด ์ด๋จ๊น์?
| current = head | ||
|
|
||
| while current: | ||
| next_node = current.next | ||
| current.next = prev | ||
| prev = current | ||
| current = next_node |
There was a problem hiding this comment.
next๊ฐ ์์ฝ์ด์ฌ์ _node๋ฅผ ๋ถ์ฌ์ฃผ์ ๊ฑฐ ๊ฐ๋ค์. ๋ค๋ฅธ ๋ค์ด๋ฐ๋ค์ด๋ ์ผ๊ด์ฑ์ ๋ง์ถ์๋ ๊ฑด ์ด๋จ๊น์?
| while char in chars: | ||
| chars.remove(s[left]) | ||
| left += 1 |
There was a problem hiding this comment.
left๋ฅผ ํ๋์ฉ ์ฆ๊ฐํ์ง ์๊ณ ๊ฐ ๋ฌธ์์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํด์ ์ ํํ๋ ๋ฐฉ์์ผ๋ก๋ ์กฐ๊ธ ๋ ์ต์ ํ๊ฐ ๊ฐ๋ฅํ๋ ํ์ด๋ณด์ ๋ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!